package com.httpClient.chapter1;

import com.JavaNet.chapter8_client_socket.SocketForHttp;
import com.sky.JULLog.MyJulLogger;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 *
 * 对比 {@link  SocketForHttp }
 *
 * @author : wushikai
 *         date : 2024-06-17
 */
public class UrlEncodeFormHttpClient {


    private static Logger log = MyJulLogger.getLogger(UrlEncodeFormHttpClient.class);


    public static void main(String[] args) throws Exception {
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");   //这样会输出代理class文件
        doRequest();
    }


    private  static void doRequest( ) throws IOException {

        HttpClient httpclient = new DefaultHttpClient();

        /*-----------------   mark 组装一个 post 请求体   -------------*/
        List<NameValuePair> formParams = new ArrayList< >();
        formParams.add(new BasicNameValuePair("sp.lk.merchant_name", "广西"));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, StandardCharsets.UTF_8);

        /*-----------------                                  -------------*/

        entity.setChunked(true);  //mark 不知道有什么用
        HttpPost httpPost = new HttpPost(  "http://localhost:8080/xy/expose/merDetail");
        httpPost.setEntity(entity);
        HttpResponse response = httpclient.execute(httpPost);  //使用 httpClient 调用 get请求
        handleResponse(response);

    }





    /**
     * 使用最简单的 流式读取
     */
    private static void handleResponse(HttpResponse response) throws IOException {

        System.out.println("response 的实现类是==>" + response.getClass().getCanonicalName());
        log.info("协议版本:" + response.getProtocolVersion());
        log.info("服务器返回状态码:" + response.getStatusLine().getStatusCode());
        log.info("服务器返回状态码表述:" + response.getStatusLine().getReasonPhrase());
        log.info("服务器返回状态描述:" + response.getStatusLine().toString());

        //使用 响应头 获取 cookie 键值对,  注意这是没有被解析过的
        Header h1 = response.getFirstHeader("Set-Cookie");
        log.info(h1.toString());
        Header h2 = response.getLastHeader("Set-Cookie");
        log.info(h2.toString());
        Header[] hs = response.getHeaders("Set-Cookie");
        log.info(hs.length + "");

        //使用 迭代器 获取 cookie 键值对 ,  httpClient 帮你解析好;
        HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator("Set-Cookie"));
        while (it.hasNext()) {
            HeaderElement elem = it.nextElement();
            log.info(elem.getName() + " = " + elem.getValue());
            NameValuePair[] params = elem.getParameters();
            for (NameValuePair param : params) {
                log.info(" " + param);
            }
        }


        HttpEntity entity = response.getEntity();   // 返回一个 HttpResponse 的 entity(实体) 这个  实体 = 响应头+响应内容

        if (entity != null) {
            InputStream inputStream = entity.getContent();   //实体对象 获取流 , 解析data 即可;
            byte[] buffer = new byte[2048];
            while (inputStream.read(buffer) != -1) {
                log.info("第一次读取的内容是:" + new String(buffer, StandardCharsets.UTF_8));
            }
            System.out.println();
            System.out.println("data  read  finished.......");
            inputStream.close();

        }
    }




}
